from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SPU, Brand, GoodsCategory

from apps.meiduo_admin.serializers.spu import SPUGoodsSerializer, BrandSerializer, SKUGoodsCategorySerializer
from apps.meiduo_admin.utils import UserPageNum


class SPUGoodsView(ModelViewSet):
    '''
        SPU表的增删改查
    '''
    # 指定查询集
    queryset = SPU.objects.all()
    # 指定序列化器
    serializer_class = SPUGoodsSerializer
    # 指定分页器
    pagination_class = UserPageNum
    # 指定权限
    permission_classes = [IsAdminUser]

    # 自定义方法获取简单品牌表数据
    def simple(self, request):
        # 1、查询品牌表获取所有品牌数据
        brands = Brand.objects.all()
        # 2、序列化返回品牌数据
        s = BrandSerializer(brands, many=True)
        return Response(s.data)

    def first(self, request):
        # 查询一级分类数据
        data = GoodsCategory.objects.filter(parent=None)
        s = SKUGoodsCategorySerializer(data, many=True)
        return Response(s.data)

    def second(self, request, pk):
        # 查询二, 三级分类数据
        data = GoodsCategory.objects.filter(parent_id=pk)
        s = SKUGoodsCategorySerializer(data, many=True)
        return Response(s.data)
